package top.someapp.dxf2dsvg.dxf;

import top.someapp.dxf2dsvg.base.Encoding;
import top.someapp.dxf2dsvg.entity.Entity;

import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.List;

/**
 * @author zwz
 * Created on 2020-04-17
 * @see <a href="http://help.autodesk.com/view/OARX/2018/CHS/?guid=GUID-235B22E0-A567-4CF6-92D3-38A2306D73F3">
 * 关于 DXF 格式 (DXF)</a> and
 * <a href="http://help.autodesk.com/view/OARX/2018/CHS/?guid=GUID-73E9E797-3BAA-4795-BBD8-4CE7A03E93CF">
 * 关于图形交换文件格式 (DXF)</a>
 */
public class Dxf {
    // dxf文件使用的编码，如code 936, utf8等
    Encoding encoding;
    // dxf文件使用的autocad版本
    String acadver;
    SectionHeader headerSection;
    SectionClasses classesSection;
    SectionTables tablesSection;
    SectionBlocks blocksSection;
    SectionEntities entitiesSection;
    SectionObjects objectsSection;

    public Encoding getEncoding() {
        // $DWGCODEPAGE ANSI_936
        if (encoding == null) {
            if (headerSection == null) {
                encoding = Encoding.UNKNOWN;
            } else {
                if ("ANSI_936".equalsIgnoreCase(headerSection.variableValue("$DWGCODEPAGE", null))) {
                    encoding = Encoding.GBK;
                } else {
                    encoding = Encoding.UTF8;
                }
            }
        }
        return encoding;
    }

    /**
     * @return 使用的autocad版本
     * @see <a href="http://help.autodesk.com/view/OARX/2018/CHS/?guid=GUID-A85E8E67-27CD-4C59-BE61-4DC9FADBE74A">HEADER 段组码 (DXF)</a>
     */
    public String getAcadver() {
        // $ACADVER AC1021
//        AC1006 = R10
//        AC1009 = R11 和 R12
//        AC1012 = R13
//        AC1014 = R14
//        AC1015 = AutoCAD 2000
//        AC1018 = AutoCAD 2004
//        AC1021 = AutoCAD 2007
//        AC1024 = AutoCAD 2010
//        AC1027 = AutoCAD 2013
//        AC1032 = AutoCAD 2018
        if (acadver == null) {
            if (headerSection != null) {
                String ver = headerSection.variableValue("$ACADVER", "AC1032");
                switch (ver) {
                    case "AC1006":
                        acadver = "R10";
                        break;
                    case "AC1009":
                        acadver = "R11/R12";
                        break;
                    case "AC1012":
                        acadver = "R13";
                        break;
                    case "AC1014":
                        acadver = "R14";
                        break;
                    case "AC1015":
                        acadver = "AutoCAD 2000";
                        break;
                    case "AC1018":
                        acadver = "AutoCAD 2004";
                        break;
                    case "AC1021":
                        acadver = "AutoCAD 2007";
                        break;
                    case "AC1024":
                        acadver = "AutoCAD 2010";
                        break;
                    case "AC1027":
                        acadver = "AutoCAD 2013";
                        break;
                    case "AC1032":
                        acadver = "AutoCAD 2018";
                        break;
                }
            }
        }
        return acadver;
    }

    public SectionHeader getHeaderSection() {
        return headerSection;
    }

    public SectionClasses getClassesSection() {
        return classesSection;
    }

    public SectionTables getTablesSection() {
        return tablesSection;
    }

    public SectionBlocks getBlocksSection() {
        return blocksSection;
    }

    public SectionEntities getEntitiesSection() {
        return entitiesSection;
    }

    public SectionObjects getObjectsSection() {
        return objectsSection;
    }

    public List<Layer> getLayers() {
        return tablesSection == null ? null : tablesSection.getLayers();
    }

    public Collection<Block> getBlocks() {
        return blocksSection == null ? null : blocksSection.getBlocks();
    }

    public Collection<Entity> getEntities() {
        return entitiesSection == null ? null : entitiesSection.getEntityList();
    }

    void onSectionFound(@Nonnull Section section) {
        if (section instanceof SectionHeader) {
            headerSection = (SectionHeader) section;
        } else if (section instanceof SectionClasses) {
            classesSection = (SectionClasses) section;
        } else if (section instanceof SectionTables) {
            tablesSection = (SectionTables) section;
        } else if (section instanceof SectionBlocks) {
            blocksSection = (SectionBlocks) section;
        } else if (section instanceof SectionEntities) {
            entitiesSection = (SectionEntities) section;
        } else if (section instanceof SectionObjects) {
            objectsSection = (SectionObjects) section;
        } else {
            System.err.println("Unsupported section: " + section);
        }
    }

    @Override
    public String toString() {
        return "Dxf{" +
                "encoding=" + getEncoding() +
                ", acadver='" + getAcadver() + '\'' +
                ", layers='" + getLayers() + '\'' +
                ", blocks='" + getBlocks() + '\'' +
                ", entities='" + getEntities() + '\'' +
                '}';
    }
}
